#!/bin/bash
#
# description: linux basic config.
#
. /etc/rc.d/init.d/functions
shopt -u expand_aliases
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
export LC_ALL=en_US.UTF-8

atest

# set variables
#
# BASEDIR=
# SOFTDIR=
# BAKSFIX=
# HOST_NAME=
# PASSWD1=
# FTPPASSWD1=
# FTPPASSWD2=
# SETYUMCACHE=
# DNSERVER1=
# DNSERVER2=
# MY_ARCH=
# REDHAT_VERSION=



set_selinux() {

        echo "set selinux..."

        [ "$(getenforce)" == Enforcing ] && setenforce 0 >/dev/null || echo "setenforce no need."

        if grep -v "#" /etc/selinux/config|grep "SELINUX=disabled" >/dev/null 2>&1; then

                echo "set selinux no need."

        else

                sed -i 's/^SELINUX=/#&/' /etc/selinux/config
                sed -i '$a\SELINUX=disabled' /etc/selinux/config

                if grep -v "#" /etc/selinux/config|grep "SELINUX=disabled" >/dev/null 2>&1; then
                        echo "set selinux success."
                else
                        $SETCOLOR_FAILURE
                        echo "set selinux failed."
                        $SETCOLOR_NORMAL
                        exit 1
                fi

        fi

}

set_timezone(){

        echo "set timezone..."
        cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
        [ $? -eq 0 ] && echo "set timezone success." || { $SETCOLOR_FAILURE; echo "set timezone failed."; $SETCOLOR_NORMAL; }

}

set_locale() {

        echo "set locale..."

        sed -i '/LANG=/c\LANG="en_US.UTF-8"' /etc/sysconfig/i18n
        locale|grep -q "LANG=en_US.UTF-8"
        [ $? -eq 0 ] && echo "set locale success." || { $SETCOLOR_FAILURE; echo "set locale failed."; $SETCOLOR_NORMAL; }

}

set_hostname() {

        echo "set hostname..."

        if [ -z "$HOST_NAME" ]; then

                $SETCOLOR_FAILURE;
                echo "env HOST_NAME empty."
                $SETCOLOR_NORMAL;

        else

                sed -i 's/^HOSTNAME/#&/' /etc/sysconfig/network
                sed -i "\$a\HOSTNAME=$HOST_NAME" /etc/sysconfig/network
                sed -i '/127/d' /etc/hosts
                sed -i "\$a\127.0.0.1\t$HOST_NAME\tlocalhost" /etc/hosts

                if grep -v "#" /etc/sysconfig/network|grep "HOSTNAME=$HOST_NAME" >/dev/null 2>&1 && grep "$HOST_NAME" /etc/hosts >/dev/null 2>&1; then

                        hostname $HOST_NAME
                        /etc/init.d/syslog  restart 1>/dev/null 2>&1
                        /etc/init.d/rsyslog restart 1>/dev/null 2>&1
                        echo "set host name success."

                else

                        $SETCOLOR_FAILURE
                        echo "set host name failed."
                        $SETCOLOR_NORMAL

                fi

        fi

}

set_func_path() {

        echo "set init.d/functions PATH env..."

        sed -i 's#PATH="/sbin:/usr/sbin:/bin:/usr/bin"#PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin#' /etc/rc.d/init.d/functions
        cat /etc/rc.d/init.d/functions|grep 'PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin' >/dev/null 2>&1 && echo "set init.d/functions PATH success." || {
                $SETCOLOR_FAILURE
                echo "set init.d/functions PATH failed."
                $SETCOLOR_NORMAL
        }

}

set_dns() {

        echo "set dns..."

        if grep -v "#" /etc/resolv.conf|grep "nameserver $DNSERVER1" >/dev/null 2>&1; then
                echo "set dns no need."
        else

                touch /etc/resolv.conf
                sed -i "\$a\nameserver $DNSERVER1" /etc/resolv.conf
                sed -i "\$a\nameserver $DNSERVER2" /etc/resolv.conf

                if grep -v "#" /etc/resolv.conf|grep "nameserver $DNSERVER1" >/dev/null 2>&1; then
                        echo "set dns success."
                else
                        $SETCOLOR_FAILURE
                        echo "set dns failed."
                        $SETCOLOR_NORMAL
                fi

        fi

}

install_package() {

        echo "install packages..."
        echo "wait please, check /var/log/tmpyum.log for progress."

        [ "$SETYUMCACHE" == 1 ] && [ ! -f /var/lock/subsys/tmpyum.lock ] && {
                touch /var/lock/subsys/tmpyum.lock
                yum clean all >/var/log/tmpyum.log 2>&1
                yum makecache >>/var/log/tmpyum.log 2>&1
        }


        yum -y install                          \
                 kernel-2.6.32-504.el6.$MY_ARCH \
           kernel-devel-2.6.32-504.el6.$MY_ARCH \
         kernel-headers-2.6.32-504.el6.$MY_ARCH \
        kernel-firmware-2.6.32-504.el6.noarch   \
        >>/var/log/tmpyum.log 2>&1;

        rpm -qa|grep kernel-2.6.32-504 >/dev/null && echo "install kernel success." || { $SETCOLOR_FAILURE; echo "install kernel failed."; $SETCOLOR_NORMAL; }


        yum -y install openssh-clients >>/var/log/tmpyum.log 2>&1;
        yum -y install openssl-devel.$MY_ARCH >>/var/log/tmpyum.log 2>&1;

        yum -y install                  \
        autoconf.noarch                 \
        automake.noarch                 \
        crontabs.noarch                 \
        dstat.noarch                    \
        epel-release.noarch             \
        iotop.noarch                    \
        yum-utils.noarch                \
                                        \
        bash.$MY_ARCH                   \
        bind-utils.$MY_ARCH             \
        cpulimit.$MY_ARCH               \
        cronie.$MY_ARCH                 \
        cronie-anacron.$MY_ARCH         \
        ctags.$MY_ARCH                  \
        vixie-cron.$MY_ARCH             \
        curl.$MY_ARCH                   \
        db4-utils.$MY_ARCH              \
        dos2unix.$MY_ARCH               \
        emacs.$MY_ARCH                  \
        unix2dos.$MY_ARCH               \
        ftp.$MY_ARCH                    \
        flex.$MY_ARCH                   \
        gcc.$MY_ARCH                    \
        gcc-c++.$MY_ARCH                \
        glibc.$MY_ARCH                  \
        glibc-headers.$MY_ARCH          \
        git.$MY_ARCH                    \
        httpd.$MY_ARCH                  \
        iproute.$MY_ARCH                \
        iptraf.$MY_ARCH                 \
        iptables.$MY_ARCH               \
        iptstate.$MY_ARCH               \
        iftop.$MY_ARCH                  \
        kexec-tools.$MY_ARCH            \
        libcgroup.$MY_ARCH              \
        libcgroup-pam.$MY_ARCH          \
        links.$MY_ARCH                  \
        lvm2.$MY_ARCH                   \
        lrzsz.$MY_ARCH                  \
        lynx.$MY_ARCH                   \
        logrotate.$MY_ARCH              \
        lsof.$MY_ARCH                   \
        jwhois.$MY_ARCH                 \
        mailx.$MY_ARCH                  \
        make.$MY_ARCH                   \
        man.$MY_ARCH                    \
        msmtp.$MY_ARCH                  \
        mutt.$MY_ARCH                   \
        ncurses.$MY_ARCH                \
        ntp.$MY_ARCH                    \
        nc.$MY_ARCH                     \
        nethogs.$MY_ARCH                \
        nmap.$MY_ARCH                   \
        openssh.$MY_ARCH                \
        openssh-server.$MY_ARCH         \
        openssl.$MY_ARCH                \
        patch.$MY_ARCH                  \
        parted.$MY_ARCH                 \
        pcre.$MY_ARCH                   \
        perl.$MY_ARCH                   \
        perl-CPAN.$MY_ARCH              \
        rsync.$MY_ARCH                  \
        sendmail.$MY_ARCH               \
        screen.$MY_ARCH                 \
        sysstat.$MY_ARCH                \
        traceroute.$MY_ARCH             \
        tree.$MY_ARCH                   \
        telnet.$MY_ARCH                 \
        tcpdump.$MY_ARCH                \
        unzip.$MY_ARCH                  \
        valgrind.$MY_ARCH               \
        vsftpd.$MY_ARCH                 \
        vim-enhanced.$MY_ARCH           \
        w3m.$MY_ARCH                    \
        which.$MY_ARCH                  \
        wget.$MY_ARCH                   \
        zip.$MY_ARCH                    \
        zlib.$MY_ARCH                   \
                                        \
        freetype.$MY_ARCH               \
        gd.$MY_ARCH                     \
        openldap.$MY_ARCH               \
        libevent.$MY_ARCH               \
        libaio.$MY_ARCH                 \
        libstdc++.$MY_ARCH              \
        libcurl.$MY_ARCH                \
        libpng.$MY_ARCH                 \
        libjpeg-turbo.$MY_ARCH          \
        libxml2.$MY_ARCH                \
                                        \
        glibc-devel.$MY_ARCH            \
        httpd-devel.$MY_ARCH            \
        ncurses-devel.$MY_ARCH          \
        openssl-devel.$MY_ARCH          \
        pam-devel.$MY_ARCH              \
        gd-devel.$MY_ARCH               \
        freetype-devel.$MY_ARCH         \
        openldap-devel.$MY_ARCH         \
        libcurl-devel.$MY_ARCH          \
        libpng-devel.$MY_ARCH           \
        libjpeg-turbo-devel.$MY_ARCH    \
        libxml2-devel.$MY_ARCH          \
        >>/var/log/tmpyum.log 2>&1;

        yum -y update --exclude=kernel* >>/var/log/tmpyum.log 2>&1;

        [ $? -eq 0 ] && echo "install packages success." || { $SETCOLOR_FAILURE; echo "install packages failed."; $SETCOLOR_NORMAL; }

}

service_config() {

        echo "stopping unnecessary services..."

        for serv in NetworkManager avahi-daemon avahi-dnsconfd bluetooth capi cups cpuspeed hplip httpd \
        irqbalance isdn ip6tables mcstrans modclusterd nfs nfslock ntpd pcscd portmap postfix portreserve \
        ricci restorecond rpcbind tcsd yum-updatesd; do
                service $serv stop 1>/dev/null 2>&1
                chkconfig --level 2345 $serv off 1>/dev/null 2>&1
        done

        for serv in atd anacron acpid crond haldaemon iptables messagebus network rsyslog sshd sendmail smartd syslog vsftpd; do

                # to prevent ssh from being kicked out
                if [ "$serv" != "iptables" ]; then
                    service $serv start 1>/dev/null 2>&1
                fi

                chkconfig --level 2345 $serv on 1>/dev/null 2>&1

        done

        [ $? -eq 0 ] && echo "services config success." || { $SETCOLOR_FAILURE; echo "services config failed."; $SETCOLOR_NORMAL; }

}

time_sync() {

        echo "time sync..."

        ntpdate ntp.api.bz 1>/dev/null 2>&1
        [ $? -eq 0 ] && echo "time sync success." || { $SETCOLOR_FAILURE; echo "time sync failed."; $SETCOLOR_NORMAL; }

}

set_ulimit() {

        echo "set ulimit..."

        ulimit -n 65536
        [ $? -eq 0 ] || { $SETCOLOR_FAILURE; echo "exec ulimit -n 65536 failed."; $SETCOLOR_NORMAL; }

        if grep -v "#" /etc/security/limits.conf|grep "* soft nofile 65536" >/dev/null 2>&1; then
                echo "set ulimit no need."
        else
                sed -i "\$a\* soft nofile 65536" /etc/security/limits.conf
                sed -i "\$a\* hard nofile 65536" /etc/security/limits.conf

                if grep -v "#" /etc/security/limits.conf|grep "* soft nofile 65536" >/dev/null 2>&1; then
                        echo "set ulimit success."
                else
                        $SETCOLOR_FAILURE
                        echo "set ulimit failed."
                        $SETCOLOR_NORMAL
                fi
        fi

}

set_kdump_kernel_param() {

        echo "set kdump kernel params..."

        sed -i 's/crashkernel=auto/crashkernel=128M/g' /etc/grub.conf
        cat /etc/grub.conf|grep "crashkernel=128M" >/dev/null 2>&1 && echo "set kdump kernel params success." || {

                $SETCOLOR_FAILURE
                echo "set kdump kernel params failed."
                $SETCOLOR_NORMAL

        }

}

batch_config() {

        echo "batch config services..."


        for f in $(find linux_basic/etc/ -type f|grep -v bak); do
            f=$(echo $f|sed 's/^linux_basic//')
            [ -f $f ] && cp -a $f $f.$BAKSFIX
        done
        [ $? -eq 0 ] && echo "backup batch config files success." || { $SETCOLOR_FAILURE; echo "backup batch config files failed."; $SETCOLOR_NORMAL; }
        cp -a linux_basic/etc/* /etc/
        [ $? -eq 0 ] && echo "cover batch config files success." || { $SETCOLOR_FAILURE; echo "cover batch config files failed."; $SETCOLOR_NORMAL; }


        rm -f /etc/cron.daily/logrotate || { $SETCOLOR_FAILURE; echo "delete anacron logrotate conf failed."; $SETCOLOR_NORMAL; }
        chmod 600 /etc/cron.d/general.cron
        [ $? -eq 0 ] && echo "set crontab success." || { $SETCOLOR_FAILURE; echo "set crontab failed."; $SETCOLOR_NORMAL; }


        for f in /etc/logrotate.d/*_logrt; do
                logrotate -f $f >/dev/null 2>&1
        done
        [ $? -eq 0 ] && echo "set logrotate success." || { $SETCOLOR_FAILURE; echo "set logrotate failed."; $SETCOLOR_NORMAL; }


        if grep -v "#" /etc/modprobe.d/custom_mod.conf|grep -q "options ipv6 disable=1"; then
                echo "disable ipv6 success."
                echo "You need reboot to disable ipv6."
        else
                $SETCOLOR_FAILURE
                echo "disable ipv6 failed."
                $SETCOLOR_NORMAL
        fi


        [ -f /etc/profile.d/custom.sh ] && echo "set profile success." || {
                $SETCOLOR_FAILURE
                echo "set profile failed."
                $SETCOLOR_NORMAL
        }


        chmod -R 755 /etc/init.d/
        [ -x /etc/init.d/rsyncd ] || { $SETCOLOR_FAILURE; echo "Copy init.d script failed."; $SETCOLOR_NORMAL; }
        for f in $(ls linux_basic/etc/rc.d/init.d/); do
                [ $f == "functions" ] && continue
                [ $f == "jfunctions" ] && continue
                [ $f == "jefunctions" ] && continue
                chkconfig --add $f
                chkconfig --level 2345 $f on 1>/dev/null 2>&1
        done
        chkconfig --list|grep -q rsyncd
        [ $? -eq 0 ] && echo "add onstart exec success." || { $SETCOLOR_FAILURE; echo "add onstart exec failed."; $SETCOLOR_NORMAL; }


        sysctl -p >/dev/null 2>&1
        if sysctl -a|grep -q "vm.swappiness = 10"; then
                echo "set kernel params success."
        else
                $SETCOLOR_FAILURE
                echo "set kernel params failed."
                $SETCOLOR_NORMAL;
        fi


        echo "batch config services all done."

}

add_user() {

        echo "add user..."

        useradd www         -M -s /sbin/nologin 2>/dev/null
        useradd tomcat      -M -s /sbin/nologin 2>/dev/null
        useradd mysql       -M -s /sbin/nologin 2>/dev/null
        useradd virtual     -M -s /sbin/nologin -d /data/ftpdata/virtual 2>/dev/null
        # useradd backup      -M -s /sbin/nologin -d /data/backup
        # useradd update      -M -s /sbin/nologin -d /data/update

        echo "$PASSWD1"    |passwd --stdin root
        # echo "$FTPPASSWD1" |passwd --stdin backup
        # echo "$FTPPASSWD1" |passwd --stdin update
        # chown -R backup.backup /data/backup
        # chown -R update.update /data/update

        cat /etc/passwd|grep www >/dev/null 2>&1
        [ $? == 0 ] && echo "add user success." || { $SETCOLOR_FAILURE; echo "add user failed."; $SETCOLOR_NORMAL; }

}

create_dir() {

        echo "create dir..."

        mkdir -p \
        /etc/cron.minutely \
        /etc/vsftpd \
        /etc/rsync \
        /etc/unison \
        /root/script \
        /root/setup \
        /root/backup \
        /root/tmp \
        /data/rsync \
        /data/setup \
        /data/backup \
        /data/pmntbak \
        /data/tmpbak \
        /data/update \
        /data/tmp \
        /data/log \
        /data/mysql \
        /data/ftpdata \
        /data/wwwroot \
        /data/rsync/setup \
        /data/rsync/soft \
        /data/rsync/script \
        /data/log/monitorlog \
        /data/log/daemonlog \
        /data/log/nginxlog \
        /data/log/tomcatlog \
        /data/log/streaminglog/cms \
        /data/log/streaminglog/easydarwin \
        /data/mysql/mysql_data_3306 \
        /data/mysql/mysql_log \
        /data/mysql/log-bin \
        /data/mysql/relay-log \
        /data/ftpdata/virtual \
        /var/run/rsync \
        /var/run/nginx \
        /var/run/tomcat \
        /var/run/php-fpm \
        /var/run/perl-fcgi \
        /var/run/mysql \
        /var/run/cms \
        /var/run/easydarwin \
        /var/lib/mysql \
        /var/log/sudo \
        /var/log/dstat \
        /var/log/rsync \
        /var/log/vsftp \
        /var/log/php-fpm \
        /var/log/msmtp \
        /var/log/mutt \
        /var/log/mysqldump \
        ;

        [ $? -eq 0 ] && echo "create dir success." || { $SETCOLOR_FAILURE; echo "create dir failed."; $SETCOLOR_NORMAL; exit 1; }

}

copy_script() {

        echo "copy script..."

        cp -f linux_basic/script/* $BASEDIR/
        chmod -R 755 $BASEDIR/*.sh
        ln -snf $BASEDIR/send-mail.sh /usr/local/bin/sdmail
        ln -snf $BASEDIR/limgr.sh     /usr/local/bin/limgr

        [ -x $BASEDIR/tool.sh ] && echo "copy script success." || { $SETCOLOR_FAILURE; echo "copy script failed."; $SETCOLOR_NORMAL; exit 1; }

}



$SETCOLOR_WARNING
echo -e "\n\nConfig linux basic..."
$SETCOLOR_NORMAL
sleep 1s

[ -z "$BASEDIR" -o -z "$MY_ARCH" ] && { $SETCOLOR_FAILURE; echo "Error: set variables first."; $SETCOLOR_NORMAL; exit 1; }

# must first and in order
set_selinux
set_timezone
set_locale
set_hostname
set_func_path
# set_dns
install_package
service_config
time_sync

set_ulimit
set_kdump_kernel_param
batch_config

# last
add_user
create_dir
copy_script

$SETCOLOR_SUCCESS
echo "Config linux basic all done."
$SETCOLOR_NORMAL
